package com.taomu.app.zongchouhuli.moudle.home.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.http.res.ResOrderInfoEntity;
import com.taomu.app.zongchouhuli.http.res.ResSubmitOrderEntity;
import com.taomu.app.zongchouhuli.moudle.home.contract.HomeOrderContract;
import com.taomu.app.zongchouhuli.moudle.home.contract.impl.HomeOrderPresenter;
import com.taomu.app.zongchouhuli.utils.dialog.PayPasswordDialog;

/**
 * 提交订单
 */
public class HomeOrderSubmitActivity extends BaseActivity<HomeOrderContract.Presenter> implements HomeOrderContract.View {

    private ImageView mBackIm;

    private TextView mOrderNumTv;

    private TextView mStateTv;

    private ImageView mIconIm;

    private TextView mProductNameTv;

    private TextView mIncomeTv;

    //顶部，价格
    private TextView mPriceTv;

    //收益率，类型
    private TextView mBanlanceTv;

    //底部，价格
    private TextView mBottomPriceTv;

    private TextView mSumbitTv;

    public final static String PRODUCTID = "product_id";

    public final static String QUANTITY = "quantity";

    public final static String INCOMETYPE = "income_type";

    public final static String PRODUCTNAME = "PRODUCTNAME";

    public final static String PRODUCTRADIO = "productradio";

    public final static String PRODUCTMONEY = "productmoney";

    public final static String DISTRIBUTION = "distribution";

    private String distribution = "";

    private TextView numTv;

    private final static String ICONIMG = "iconimg";

    public static void start(Context mContext,String productid, String quantity, String incometype, String productname,String productradio,String money, String distribution,String iconImg){
        Intent intent = new Intent(mContext,HomeOrderSubmitActivity.class);
        intent.putExtra(PRODUCTID,productid);
        intent.putExtra(QUANTITY,quantity);
        intent.putExtra(INCOMETYPE, incometype);
        intent.putExtra(PRODUCTNAME, productname);
        intent.putExtra(PRODUCTRADIO, productradio);
        intent.putExtra(PRODUCTMONEY, money);
        intent.putExtra(ICONIMG, iconImg);
        intent.putExtra(DISTRIBUTION, distribution);
        mContext.startActivity(intent);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_home_submit_order;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.order_submit_back_im);
        numTv = findViewById(R.id.order_submit_num);
        mOrderNumTv = findViewById(R.id.order_submit_num_tv);
        mStateTv = findViewById(R.id.order_submit_state_tv);
        mIconIm = findViewById(R.id.order_submit_icon_im);
        mIncomeTv = findViewById(R.id.order_submit_income_tv);
        mProductNameTv = findViewById(R.id.order_submit_name_tv);
        mPriceTv = findViewById(R.id.order_submit_price_tv);
        mBanlanceTv = findViewById(R.id.order_submit_balance_type_tv);
        mBottomPriceTv = findViewById(R.id.order_submit_bottom_price_tv);
        mSumbitTv = findViewById(R.id.order_submit_submit_tv);
    }

    private String productid = "-1";

    private String orderid = "0";

    @Override
    protected void initData() {
        if(getIntent() != null){
            if(!TextUtils.isEmpty(getIntent().getStringExtra(PRODUCTID))) {
                productid = getIntent().getStringExtra(PRODUCTID);
            }
            if(!TextUtils.isEmpty(getIntent().getStringExtra(DISTRIBUTION))) {
                distribution = getIntent().getStringExtra(DISTRIBUTION);
            }
            String num = getIntent().getStringExtra(QUANTITY);
            String incometype = getIntent().getStringExtra(INCOMETYPE);
            String incomeraido = getIntent().getStringExtra(PRODUCTRADIO);
            String name = getIntent().getStringExtra(PRODUCTNAME);
            String money = getIntent().getStringExtra(PRODUCTMONEY);
            String iconimg = getIntent().getStringExtra(ICONIMG);
            Glide.with(this).load(iconimg).into(mIconIm);
            mPriceTv.setText("¥"+money);
            mPriceTv.setText("¥"+money);
            mProductNameTv.setText(name);
            mIncomeTv.setText("最高收益"+incomeraido);
            if("day_income".equals(incometype)){
                mBanlanceTv.setText("日结"+incomeraido);
            }else if("week_income".equals(incometype)){
                mBanlanceTv.setText("周结"+incomeraido);
            }else if("month_income".equals(incometype)){
                mBanlanceTv.setText("月结"+incomeraido);
            }
            numTv.setText("X"+num);
            mPresenter.getOrderInfo(productid, num, incometype,distribution);
        }
    }

    private String content;

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mSumbitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HomePayActivity.start(HomeOrderSubmitActivity.this, productid, String.valueOf(orderid), String.valueOf(orderid), totalMoney, distribution);
                finish();
                //todo:首先判断有没有支付密码
//                PayPasswordDialog dialog = new PayPasswordDialog(HomeOrderSubmitActivity.this);
//                dialog.setContent(content);
//                dialog.setPayPasswordListener(new PayPasswordDialog.PayPasswordListener() {
//                    @Override
//                    public void onFinishInput(String pwd) {
//                        mPresenter.setsubmit(orderid, pwd, productid,distribution);
//                    }
//                });
//                dialog.show();
            }
        });
    }

    @Override
    protected HomeOrderContract.Presenter createPresenter() {
        return new HomeOrderPresenter(this);
    }

    private String ordernum;

    private String totalMoney;

    @Override
    public void setOrderInfo(ResSubmitOrderEntity entity) {
        orderid = String.valueOf(entity.getOrder_id());
        mOrderNumTv.setText(String.valueOf(entity.getOrder_id()));
        mBottomPriceTv.setText("¥"+entity.getTotal_price());
        content = "¥"+entity.getTotal_price();
        totalMoney = String.valueOf(entity.getTotal_price());
    }

    @Override
    public void setSubmitResults(String msg) {

    }

    @Override
    public void setPresenter(HomeOrderContract.Presenter presenter) {
        Intent intent = new Intent(HomeOrderSubmitActivity.this, HomePayDetailActivity.class);
        startActivity(intent);
        finish();
    }
}
